package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper  baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


	@Override
	public List<JSONObject> findAllCategory() {


		//一级分类集合，需要返回的结果
		List<JSONObject> finalList =new ArrayList<>();


		//从视图查询所有数据
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);

		//获取一级id和对应集合的Map
		Map<Long,List<BaseCategoryView>> category1IdMap=baseCategoryViewList.stream().collect(Collectors
				.groupingBy(BaseCategoryView::getCategory1Id));

		//循环遍历一级分类map
		for (Map.Entry<Long, List<BaseCategoryView>> entry : category1IdMap.entrySet()) {
			//获取map里的每个键值对
			Long category1Id = entry.getKey();
			List<BaseCategoryView> category1ViewList = entry.getValue();
			//把他放入JSONObject对象中。
			//首先放入一级分类的id和name，然后是一个二级分类的集合，需要组装一下
			JSONObject category1=new JSONObject(true);
			category1.put("categoryId", category1Id);
			category1.put("categoryName",category1ViewList.get(0).getCategory1Name());

			//按照二级分类Id进行分组。
			Map<Long, List<BaseCategoryView>> category2IdMap = category1ViewList.stream()
					.collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));

			// 声明二级分类对象集合
			List<JSONObject> category2Child = new ArrayList<>();

			for (Map.Entry<Long, List<BaseCategoryView>> entry2 : category2IdMap.entrySet()) {
				JSONObject category2=new JSONObject(true);
				//获取map里的每个键值对
				Long category2Id=entry2.getKey();
				List<BaseCategoryView> category2ViewList=entry2.getValue();
				//把他放入JSONObject对象中。
				//首先放入二级分类的id和name，然后是一个三级分类的集合，需要组装一下
				category2.put("categoryId", category2Id);
				category2.put("categoryName",category2ViewList.get(0).getCategory2Name());

				//因为三级分类Id不需要去重，直接从集合中取出生成新的集合。
				List<JSONObject> category3List = category2ViewList.stream().map((baseCategoryView) -> {
					JSONObject category3 = new JSONObject(true);
					category3.put("categoryId", baseCategoryView.getCategory3Id());
					category3.put("categoryName", baseCategoryView.getCategory3Name());
					return category3;
				}).collect(Collectors.toList());
				//三级放入二级
				category2.put("categoryChild",category3List);
				//单个二级JSONObject放入集合
				category2Child.add(category2);
			}
			//二级放入一级
			category1.put("categoryChild",category2Child);
			//单个一级放入集合
			finalList.add(category1);
		}

		return finalList;
	}

	@Override
	public List<BaseAttribute> findAttribute(Long category1Id) {
		return  baseAttributeMapper.selectAttributeBycategory1Id(category1Id);
	}
}
